package cn.exrick.controller;

import cn.exrick.common.exception.NeedLoginException;
import cn.exrick.common.exception.XmallException;
import cn.exrick.pojo.TbQuestion;
import cn.exrick.pojo.TbQuestionCat;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.front.Member;
import cn.exrick.pojo.front.QuestionCommentResult;
import cn.exrick.pojo.front.QuestionResult;
import cn.exrick.service.LoginService;
import cn.exrick.service.QuestionCatService;
import cn.exrick.service.QuestionService;
import cn.exrick.utils.ResultUtil;
import cn.exrick.utils.mybatis.Page;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/question")
@Api(description= "技术问答")
public class QuestionFrontController {

    @Autowired
    private QuestionCatService questionCatService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取问答列表")
    public Result<Page<QuestionResult>> getQuestionList(String token, @RequestParam(defaultValue = "0") int page,
                                                     @RequestParam(defaultValue = "5") int size, Integer type, Long category, String search) {
        page++;
        Long memberId = 0L;
        if(null != type){
            Member member = loginService.getUserByToken(token);
            if (member == null) {
                throw new NeedLoginException();
            }
            memberId = member.getId();
        }

        Page<QuestionResult> questionResultPage = questionService.getQuestionPageForApi(page, size, memberId, type, category, search);
        return new ResultUtil<Page<QuestionResult>>().setData(questionResultPage);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取问答详情")
    public Result<QuestionResult> getQuestionDetail(Long questionId) {
        if(null == questionId){
            throw new XmallException("问答ID不能为空");
        }
        QuestionResult result = questionService.getDetailForApi(questionId);
        if(null == result){
            throw new XmallException("问答不存在");
        }
        return new ResultUtil<QuestionResult>().setData(result);
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiOperation(value = "提交问题")
    public Result<Object> save(String token, String title, String content, Long category, String image){
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        if(StrUtil.isBlank(title)){
            throw new XmallException("提问标题不能为空");
        }
        if(StrUtil.isBlank(content)){
            throw new XmallException("提问内容不能为空");
        }
        if(null == category){
            throw new XmallException("提问类型不能为空");
        }

        TbQuestion tbQuestion = new TbQuestion();
        tbQuestion.setTitle(title);
        tbQuestion.setContent(content);
        tbQuestion.setCatId(category);
        tbQuestion.setImage(image);
        tbQuestion.setMemberId(member.getId());
        tbQuestion.setCreated(new Date());

        questionService.save(tbQuestion);

        return new ResultUtil<Object>().setData(true);
    }

    @RequestMapping(value = "/cat/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有问答分类")
    public Result<List<TbQuestionCat>> getAll(){
        List<TbQuestionCat> list = questionCatService.getAll();
        return new ResultUtil<List<TbQuestionCat>>().setData(list);
    }

    @RequestMapping(value = "/comment/list", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取问答评论列表")
    public Result<Page<QuestionCommentResult>> getQuestionCommentList(String token, @RequestParam(defaultValue = "0") int page,
                                                     @RequestParam(defaultValue = "5") int size, Long questionId) {
        page++;
        if(null == questionId){
            throw new XmallException("问答ID不能为空");
        }
        Page<QuestionCommentResult> commentResultPage = questionService.getQuestionCommentPageForApi(page, size, questionId);
        return new ResultUtil<Page<QuestionCommentResult>>().setData(commentResultPage);
    }

    @RequestMapping(value = "/comment/submit", method = RequestMethod.POST)
    @ApiOperation(value = "提交问答评论")
    public Result<Object> saveComment(String token, Long questionId, String content) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        if (StringUtils.isBlank(content)) {
            throw new XmallException("评论内容不能为空");
        }
        if(null == questionId){
            throw new XmallException("问答ID不能为空");
        }
        QuestionResult result = questionService.getDetailForApi(questionId);
        if(null == result){
            throw new XmallException("问答不存在");
        }

        questionService.saveComment(member.getId(), questionId, content);

        return new ResultUtil<Object>().setData(null);
    }


}
